package com.yanqu.road.server.cmd.activity.jieshi.cross;

import com.yanqu.road.entity.activity.jieshi.config.JieShiConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.JieShiProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.jieshi.JieShiMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import io.netty.channel.ChannelHandlerContext;

@Cmd(code = Protocol.S_JIESHI_CROSS_VISIT_OTHER, desc = "跨服拜访他人街市")
public class CrossJieShiVisitOtherCmd implements Command {

    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        JieShiProto.CrossJieShiVisitToOtherGameServerReqMsg reqMsg = JieShiProto.CrossJieShiVisitToOtherGameServerReqMsg.parseFrom(packet.getBytes());
        JieShiProto.CrossJieShiVisitOtherRespMsg.Builder builder = JieShiProto.CrossJieShiVisitOtherRespMsg.newBuilder();
        JieShiProto.JieShiVisitOtherRespMsg.Builder respMsg = JieShiProto.JieShiVisitOtherRespMsg.newBuilder();
        JieShiConfig config = JieShiMgr.getConfig();
        if (config == null || config.getActivityInfo() == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            respMsg.setRet(GameErrorCode.E_JIESHI_ACTIVITY_NOT_IN_TIME);
            builder.setMsg(respMsg);
        } else {
            builder.setActivityId(config.getActivityInfo().getActivityId());
            builder.setMsg(JieShiMgr.visitOther(reqMsg.getVisitPlayerId()));
        }
        builder.setOriginPlayerId(reqMsg.getOriginPlayerId());
        builder.setOriginServerId(reqMsg.getOriginServerId());
        YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_CROSS_VISIT_OTHER_JIE_SHI_RESP, builder);
        GamePlayerMgr.sendPacket(0, message);
    }

}
